/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Amine
 */
public class Server {
    
    static final int PORT = 8888;
    static final int BUFFER_SIZE = 1024;
    
    public static void main(String[] args) {
        // TODO code application logic here
        DatagramSocket socket = null;
        DatagramPacket packet;
        byte[] receivedDataJSON = new byte[BUFFER_SIZE];
        String receivedDataString = null;
        
        Gson gson = new GsonBuilder().create();
        try {
            socket = new DatagramSocket(PORT);
        } catch (SocketException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        while(true){
            System.out.println("Starting Server on port "+ PORT);
            packet = new DatagramPacket(receivedDataJSON, BUFFER_SIZE);
            
            try {
                socket.receive(packet);
                receivedDataString = new String(receivedDataJSON, "UTF-8");
                System.out.println(receivedDataString);
               
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            ReleveTemperature releve = gson.fromJson(receivedDataString, ReleveTemperature.class);
            System.out.println(releve.getDate());
            System.out.println(releve.getLocation());
            System.out.println(releve.getTemperature());
                       
        }
    }
    
}
